<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入机型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IMEI" prop="iemi">
        <el-input
          v-model="queryParams.iemi"
          placeholder="请输入手机编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="orderList">
      <el-table-column label="编号" type="index" align="center" />
      <el-table-column label="机型名称" align="center" prop="modelName" />
      <el-table-column label="IMEI" align="center" prop="iemi" />
      <el-table-column label="店员" align="center" prop="operatorName" />
      <el-table-column label="门店" align="center" prop="shopName" />
      <el-table-column label="公司" align="center" prop="companyName" />
      <el-table-column label="回收商" align="center" prop="recycleBusName" />
      <el-table-column label="报价师" align="center" prop="recycleOperatorName" />
      <el-table-column label="回收金额" align="center" prop="price" />
      <el-table-column label="回收时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag
            v-for="item in statusOptions"
            :type="item.t" v-if="item.v === scope.row.status">{{item.l}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退货情况" align="center" prop="isRefund">
        <template v-slot="scope">
          <span v-if="scope.row.isRefund === 0">无</span>
          <span v-else>已退货</span>
        </template>
      </el-table-column>
      <el-table-column label="追差情况" align="center" prop="isCatchUp">
        <template v-slot="scope">
          <span v-if="scope.row.isCatchUp === 0">无</span>
          <span v-else>已补差</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-line-square"
            @click="handleMessage(scope.row)"
          >对话</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="询价详情" :visible.sync="open" width="500px" append-to-body>
      <el-form v-if="form"  ref="form" :model="form" label-width="80px">
        <el-form-item label="机型">
          {{form.modelName}}
        </el-form-item>
        <el-form-item label="IMEI">
          {{form.iemi}}
        </el-form-item>
        <el-form-item label="验机">
          {{form.verifyModelStrings}}
        </el-form-item>
        <el-form-item label="图片">
          <div>
            <el-image
              style="width: 100px;height: 100px;margin: 10px"
              :src="item" v-for="(item, key) in form.fileUrls"
              :preview-src-list="form.fileUrls"
              :initial-index="key"
            />
          </div>
        </el-form-item>

      </el-form>
    </el-dialog>

    <el-dialog title="对话详情" :visible.sync="msgOpen" width="500px" append-to-body>
      <div v-for="item in msgList" class="msg-box">
        <div class="user-box">发送人:
          <span v-if="item.sendUser === msgRow.offerOperatorId">{{msgRow.offerOperatorName}}</span>
          <span v-if="item.sendUser === msgRow.clerkId">{{msgRow.clerkName}}</span>
        </div>
        <div style="display: flex; flex-direction: row">
          <div class="left-msg-box" v-if="item.msgType === 1">
            <span>{{item.content}}</span>
          </div>
          <div v-else>
            <el-image :src="prefixUrl + item.content" style="width: 100px;height: 100px" :preview-src-list="getMsgImageList()" :initial-index="getImageKey(item)"></el-image>
          </div>
          <div class="read-box">已读</div>
        </div>
        <div class="user-box">{{parseTime(item.sendTime) }}</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrder} from "@/api/comment/inquiryOrder";
import {listMessage} from  "@/api/comment/offerOrder"
import {parseTime} from "@/utils/ruoyi";
import {listRecycleOrder} from "@/api/comment/recycleOrder";

export default {
  name: "Offer",
  data() {
    return {
      prefixUrl: 'https://www.dajinghui.com/prod-api',
      /*1：待付款  2：待发货  3：待收货  4：已完成  5：取消*/
      statusOptions: [
        {l:'待付款', v:1, t:'danger'},
        {l:'待发货', v:2, t:'warning'},
        {l:'待收货', v:3, t:'primary'},
        {l:'已完成', v:4, t:'success'},
        {l:'取消', v:5, t:'info'}
      ],
      /*消息对话框开关*/
      msgOpen: false,
      /*消息列表*/
      msgList: [],
      /*查看的消息行 订单信息*/
      msgRow: null,
      form: null,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 询价订单表格数据
      orderList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
        iemi: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    getImageKey(row) {
      let key = 0;
      for (let i = 0; i < this.msgList.length; i++) {
        if(this.msgList[i].msgType === 2){
          if(this.msgList[i].id === row.id) {
            return key;
          }else {
            key++;
          }
        }
      }

      return key;
    },
    /*获取消息图片列表*/
    getMsgImageList() {
      let arr = [];
      this.msgList.forEach(item => {
        if(item.msgType === 2)
          arr.push(this.prefixUrl + item.content)
      })

      return arr;
    },
    /*消息对话*/
    handleMessage(row) {
      this.msgRow = row;
      const query = {
        inquiryOrderId: row.inquiryOrderId,
        offerOrderId: row.offerId
      }

      listMessage(query).then(res => {
        this.msgList = res.rows;
        this.msgOpen = true;
      })
    },
    /** 查询询价订单列表 */
    getList() {
      this.loading = true;
      listRecycleOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleDetail(row) {
      getOrder(row.inquiryOrderId).then(res => {
        this.form = res.data;
        this.open = true;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>

<style scoped lang="scss">

.msg-box {
  margin: 20px 10px;
  border-bottom: dashed 1px #DCDFE6 ;
  .user-box {
    font-size: 12px;
    color: #909399;
  }
  .left-msg-box {
    padding: 10px;
    border-radius: 5px;
    background-color: #F2F6FC;
    margin: 10px 0;
  }
  .read-box {
    display: flex;
    font-size: 10px;
    color: #909399;
    margin-left: 5px;
    align-content: center;
    align-items: center;
  }
}
</style>
